import { ref } from 'vue';
import { Dialog } from 'quasar';

import ExcelImport from 'src/components/ExcelImport.vue';

/**
 * 打开dialog需要的窗口参数
 *
 * @param dialogTitle 窗口标题，不填默认窗口
 */
export const useDialog = (dialogTitle?: string) => {
  const dialog = ref(false);
  const title = ref(dialogTitle || '窗口');
  const closeDialog = () => (dialog.value = false);

  return { dialog, title, closeDialog };
};

/**
 * 打开新增编辑窗口所需参数和方法
 *
 * @param dialogTitle 窗口标题；
 * @returns
 */
export const useFormDialog = (dialogTitle: string) => {
  const { dialog, title, closeDialog } = useDialog(dialogTitle);
  const id = ref<IdType>(undefined);

  const handleAdd = (addTitle?: string) => {
    title.value = addTitle || '新增' + dialogTitle;
    id.value = undefined;
    dialog.value = true;
  };

  const handleEdit = (objId: IdType, editTitle?: string) => {
    title.value = editTitle || '编辑' + dialogTitle;
    id.value = objId;
    dialog.value = true;
  };

  return { dialog, title, closeDialog, id, handleAdd, handleEdit };
};

/**
 * 打开导入窗口执行导入功能
 * @param importKey 导入类型键值
 * @param params 导入所需的业务参数
 * @param callBack 导入成功后的回调
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function handleExcelImport(
  importKey: string,
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  callBack: (params?: any) => void = () => {},
  params: { title?: string; maxFileSize?: number; [prop: string]: string | number | undefined } = {},
) {
  const maxFileSize = params.maxFileSize;
  delete params.maxFileSize;
  const title = params.maxFileSize;
  delete params.title;

  Dialog.create({ component: ExcelImport, componentProps: { importKey, title, maxFileSize, params } }).onOk(callBack);
}
